package com.jqb.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HttpCookies extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		/*添加Cookie*/
		Cookie cookie = new Cookie("name","admin");
		cookie.setMaxAge(60*60);//1小时后销毁,如果是负数表示浏览器关闭就销毁
		response.addCookie(cookie);
		
		/*删除指定Cookie*/
//		Cookie[] cookies = request.getCookies();
//		for(Cookie c:cookies){
//			if("name".equals(c.getName())){
//				cookie.setMaxAge(0);
//				response.addCookie(cookie);
//			}
//		}
		
		/*获取Cookie*/
//		Cookie[] cookies = request.getCookies();
//		for(Cookie c:cookies){
//			System.out.println(c.getName()+":"+c.getValue());
//		}
		
		request.getRequestDispatcher("Http").forward(request, response);
	}
}
